package com.china08.yxyapi.service.weike;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;

@Service
public class WeikeServiceImpl implements WeikeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WeikeServiceImpl.class);

    @Autowired
    private ElasticsearchTemplate et;

    @Override
    public Page<ElasticYxyResResource> getPage(String stage, Pageable pageable) {

        String schoolId = "ff8080815ccd85bd015d828eb2b5741f";

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        TermQueryBuilder builder = new TermQueryBuilder("stage", stage);
        boolQueryBuilder.must(builder);
        // 微课类型
        TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
        boolQueryBuilder.must(builderType);
        // 指定学校
        TermQueryBuilder builderSchool = new TermQueryBuilder("uploaderSchoolId", schoolId);
        boolQueryBuilder.must(builderSchool);
        // 审核通过
        TermQueryBuilder builderStatus = new TermQueryBuilder("status", 1);
        boolQueryBuilder.must(builderStatus);
        // 非删除
        TermQueryBuilder builderDel = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder.must(builderDel);
        // 非私有的
        TermQueryBuilder builderviewScope1 = new TermQueryBuilder("viewScope", 0);
        boolQueryBuilder.mustNot(builderviewScope1);

        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageable)
                .build();
        Page<ElasticYxyResResource> respPage = et.queryForPage(searchQuery, ElasticYxyResResource.class);

        LOGGER.info(boolQueryBuilder.toString());

        return respPage;
    }

}
